package com.jiangnan.artstudio.fragment;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import com.jiangnan.artstudio.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PublishDemandFragment extends BaseFragment {

    private EditText btnLeft1,btnCenter1,btnRight1,btnLeft2,btnCenter2,btnRight2;

    private EventFragment ef;
    private FragmentManager fManager;

    public ImageView returnBtn;

    private GridView gridViewTag;
    private GridView gridViewDetail;

    private List<Map<String,Object>> dataListTag;
    private List<Map<String,Object>> dataListDetail;

    private String[] tagTxt = {"网页","平面","UI","动画","室内","服装","摄影","+"};
    private int[] iconLogo = {R.drawable.workroom,R.drawable.publish_img_clock,R.drawable.mine,R.drawable.workroom};
    private String[] detailTxt = {"所属工作室","项目所需时间","指导老师","需要人员人数"};

    private SimpleAdapter adapterTag;
    private SimpleAdapter adapterDetail;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_publish_demand,container,false);

        fManager = getFragmentManager();

        findViewId(view);

        initTagGridView();

        initDetailGridView();

        initTitle(view);

        onClick();
        return view;
    }

    private void initDetailGridView() {
        dataListDetail = new ArrayList<>();
        adapterDetail =new SimpleAdapter(getActivity(),getDataDetails(),R.layout.grid_item_publish_details,
                new String[]{"logo","txt"},
                new int[]{R.id.publish_detail_logo,R.id.publish_detail_txt});
        gridViewDetail.setAdapter(adapterDetail);
    }

    public void initTagGridView() {
        dataListTag = new ArrayList<>();
        adapterTag =new SimpleAdapter(getActivity(),getDataTag(),R.layout.grid_item_publish_tag,
                new String[]{"txt"},
                new int[]{R.id.publish_tag_txt});
        gridViewTag.setAdapter(adapterTag);
    }

    public List<Map<String,Object>> getDataTag(){
        for(int i=0;i<tagTxt.length;i++){
            Map<String,Object>map=new HashMap<>();
            map.put("txt",tagTxt[i]);
            dataListTag.add(map);
        }
        return dataListTag;
    }

    public List<Map<String,Object>> getDataDetails(){
        for(int i=0;i<iconLogo.length;i++){
            Map<String,Object>map=new HashMap<>();
            map.put("logo",iconLogo[i]);
            map.put("txt",detailTxt[i]);
            dataListDetail.add(map);
        }
        return dataListDetail;
    }

    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if(ef != null) fragmentTransaction.hide(ef);
    }

    public void onClick() {
        returnBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                FragmentTransaction fTransaction = fManager.beginTransaction();
                hideAllFragment(fTransaction);
                ef = new EventFragment();
                fTransaction.add(R.id.main_content,ef).commit();
            }
        });

        btnLeft1.setOnClickListener(new View.OnClickListener(){
            int flag=0;
            @Override
            public void onClick(View v) {
                if(flag == 0) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        btnLeft1.setBackground(getResources().getDrawable(R.drawable.publish_btn_left1));
                        flag=1;
                    }

                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        btnLeft1.setBackground(getResources().getDrawable(R.drawable.publish_btn_left2));
                        flag=0;
                    }

                }
            }
        });

        btnCenter1.setOnClickListener(new View.OnClickListener(){
            int flag=0;
            @Override
            public void onClick(View v) {
                if(flag == 0) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        btnCenter1.setBackground(getResources().getDrawable(R.drawable.publish_btn_center1));
                        flag=1;
                    }

                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        btnCenter1.setBackground(getResources().getDrawable(R.drawable.publish_btn_center2));
                        flag=0;
                    }

                }
            }
        });

        btnRight1.setOnClickListener(new View.OnClickListener(){
            int flag=0;
            @Override
            public void onClick(View v) {
                if(flag == 0) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        btnRight1.setBackground(getResources().getDrawable(R.drawable.publish_btn_right1));
                        flag=1;
                    }

                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        btnRight1.setBackground(getResources().getDrawable(R.drawable.publish_btn_right2));
                        flag=0;
                    }

                }
            }
        });

        btnLeft2.setOnClickListener(new View.OnClickListener(){
            int flag=0;
            @Override
            public void onClick(View v) {
                if(flag == 0) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        btnLeft2.setBackground(getResources().getDrawable(R.drawable.publish_btn_left1));
                        flag=1;
                    }

                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        btnLeft2.setBackground(getResources().getDrawable(R.drawable.publish_btn_left2));
                        flag=0;
                    }

                }
            }
        });

        btnCenter2.setOnClickListener(new View.OnClickListener(){
            int flag=0;
            @Override
            public void onClick(View v) {
                if(flag == 0) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        btnCenter2.setBackground(getResources().getDrawable(R.drawable.publish_btn_center1));
                        flag=1;
                    }

                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        btnCenter2.setBackground(getResources().getDrawable(R.drawable.publish_btn_center2));
                        flag=0;
                    }

                }
            }
        });

        btnRight2.setOnClickListener(new View.OnClickListener(){
            int flag=0;
            @Override
            public void onClick(View v) {
                if(flag == 0) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        btnRight2.setBackground(getResources().getDrawable(R.drawable.publish_btn_right1));
                        flag=1;
                    }

                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        btnRight2.setBackground(getResources().getDrawable(R.drawable.publish_btn_right2));
                        flag=0;
                    }

                }
            }
        });
    }

    public void findViewId(View view) {
        gridViewTag = view.findViewById(R.id.publish_grid_tag);
        gridViewDetail = view.findViewById(R.id.publish_grid_detail);
        returnBtn = view.findViewById(R.id.publish_return);

        btnLeft1 = view.findViewById(R.id.publish_btn_left_1);
        btnCenter1 = view.findViewById(R.id.publish_btn_center_1);
        btnRight1 = view.findViewById(R.id.publish_btn_right_1);

        btnLeft2 = view.findViewById(R.id.publish_btn_left_2);
        btnCenter2 = view.findViewById(R.id.publish_btn_center_2);
        btnRight2 = view.findViewById(R.id.publish_btn_right_2);
    }
}
